﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using FlashCards.SL.FlashCardCreator.DataService;
using System.Collections.ObjectModel;
using FlashCards.SL.FlashCardCreator.ViewModels.Commands;
using FlashCards.Dto;
using System.Collections.Specialized;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using FlashCards.SL.FlashCardCreator.Utils;
using System.ComponentModel;

namespace FlashCards.SL.FlashCardCreator.ViewModels
{
    public class FlashcardSetViewModel : BaseViewModel
    {
        private FlashcardSet _flashcardSet;

        public FlashcardSetViewModel(FlashcardSet set)
        {
            PropertyChanged += HandlePropertyChanged;
            _flashcardSet = set;
            InitializeCategories();
            _flashcardSet.Category = Categories.FirstOrDefault(x => x.Id == _flashcardSet.Category.Id);
            InitializeFlashcardsCollection(true);
            InitializeCommands();
            OnPropertyChanged("SelectedCategory");
        }

        void InitializeCategories()
        {
            Categories = Application.Current.Resources["Categories"] as List<Category>;
        }

        public void InitializeFlashcardsCollection(bool create)
        {
            if (create)
                Flashcards = new ObservableCollection<FlashcardViewModel>();
            else
                Flashcards.Clear();

            foreach (var item in _flashcardSet.Flashcards)
            {
                var tmpFlashcard = item;
                var flashcard = new FlashcardViewModel(item);
                flashcard.FlashcardRemoving += (o, e) =>
                    {
                        _flashcardSet.Flashcards.Remove(tmpFlashcard);
                        Flashcards.Remove(flashcard);
                    };
                Flashcards.Add(flashcard);
            }

            Flashcards.CollectionChanged += (o, e) =>
                {

                };
        }

        public void UpdateFlashcardSet(FlashcardSet set)
        {
            _flashcardSet = set;
            InitializeFlashcardsCollection(false);
            OnPropertyChanged("SelectedCategory");
        }

        public FlashcardSet FlashcardSet { get { return _flashcardSet; } }

        public string Name
        {
            get { return _flashcardSet.Name; }
            set { _flashcardSet.Name = value; OnPropertyChanged("Name"); }
        }

        public bool IsPublic
        {
            get { return _flashcardSet.IsPublic; }
            set { _flashcardSet.IsPublic = value; OnPropertyChanged("IsPublic"); }
        }

        public DateTime DateModified
        {
            get { return _flashcardSet.DateModified; }
            set { _flashcardSet.DateModified = value; OnPropertyChanged("DateModified"); }
        }

        public Category SelectedCategory
        {
            get { return _flashcardSet.Category; }
            set { _flashcardSet.Category = value; OnPropertyChanged("SelectedCategory"); }
        }

        public List<Category> Categories { get; set; }

        public ObservableCollection<FlashcardViewModel> Flashcards { get; set; }

        public string Tags
        {
            get { return _flashcardSet.Tags; }
            set { _flashcardSet.Tags = value; OnPropertyChanged("Tags"); }
        }

        #region COMMANDS

        public ICommand AddFlashcardCommand { get; private set; }

        private void InitializeCommands()
        {
            AddFlashcardCommand = new Command(AddFlashcard);
        }

        void AddFlashcard()
        {
            var flashcard = new Flashcard
            {
                Definition = new FlashcardPart() { DateModified = DateTime.Now },
                Term = new FlashcardPart() { DateModified = DateTime.Now }
            };
            _flashcardSet.Flashcards.Add(flashcard);

            var flashcardViewModel = new FlashcardViewModel(flashcard);
            flashcardViewModel.IsExpanded = true;
            flashcardViewModel.FlashcardRemoving += (o, e) =>
                {
                    _flashcardSet.Flashcards.Remove(flashcard);
                    Flashcards.Remove(flashcardViewModel);
                };

            foreach (var item in Flashcards)
                item.IsExpanded = false;

            Flashcards.Add(flashcardViewModel);
        }

        #endregion

        #region PROPERTY CHANGED

        void HandlePropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "SelectedCategory")
            {
                if (SelectedCategory != null)
                {
                    var dictionary = new AccentLettersUtils().GetAccentsForLanguage(SelectedCategory.Language);
                    if (Application.Current.Resources.Contains("AccentsLettersDictionary"))
                        Application.Current.Resources.Remove("AccentsLettersDictionary");
                    Application.Current.Resources.Add("AccentsLettersDictionary", dictionary);
                }
            }
        }

        #endregion
    }
}
